/**
 * \file vectors.c
 * \brief Interrupt vectors
 * \author Radim Badsi <radim.badsi@polytech.univ-montp2.fr>
 * \date 21/07/2010
 */
#include <ucos_ii.h>
#include <timers.h>
#include "smallsatos.h"
#include "can_rx.h"

extern OS_EVENT* can_new_message;

void CPUlowInterruptHook(void)
{
    if (INTCONbits.TMR0IF) { /* TMR0 overflow */
        INTCONbits.TMR0IF = 0; /* Clear the interrupt flag */
        OSTimeTick();
    }

	if (PIR3bits.TXBnIF)
	{
		/* CAN transfer completed, put the transceiver back to SLEEP mode */
		MCP2551_RS = 1;
	}

	/* CAN */
	if (PIR3bits.WAKIF) /* New message received */
	{
		OSSemPost(can_new_message);
		MCP2551_RS = 0; /* Wake up the transceiver */
	}

	if (PIR3bits.RXBnIF) 
	{
		/* The transceiver is already awake */
		OSSemPost(can_new_message);
	}

	if (PIR2bits.OSCFIF)
	{
		/* Main oscillator failure (the internal RC oscillator is automatically switched on) */
		PIR2bits.OSCFIF = 0; /* Disable the warning */
	}
}

#pragma interrupt CPUhighInterruptHook
void CPUhighInterruptHook(void)
{
}
